package com.gitee.poi.plugin.core.write;

import org.apache.poi.ss.usermodel.Cell;

import java.math.BigDecimal;

/**
 * @author wangxin
 * @time 2019/1/8 14:54
 */
public class DefaultWriteCellValue implements WriteCellValue {

    private int scale = 2;

    public int getScale() {
        return scale;
    }

    public void setScale(int scale) {
        this.scale = scale;
    }

    @Override
    public void setCellValue(Cell cell, Object value) {
        Class clazz = value.getClass();
        if (Number.class.isAssignableFrom(clazz) || double.class.equals(clazz) || float.class.equals(clazz) || int.class.equals(clazz) || long.class.equals(clazz)) {
            BigDecimal v = new BigDecimal(String.valueOf(value)).setScale(scale,BigDecimal.ROUND_HALF_UP);
            cell.setCellValue(v.doubleValue());
        } else if (Boolean.class.isAssignableFrom(clazz) || boolean.class.equals(clazz)) {
            cell.setCellValue((boolean) value);
        } else {
            cell.setCellValue(value.toString());
        }
    }
}
